//
//  DrawMoneyViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/10.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "DrawMoneyViewController.h"
#import "SafeCertifyView.h"
#import "DrawMoneySureView.h"
#import "DetailedAddressViewController.h"
#import "LoginSafeTableViewController.h"
#import "ScanViewController.h"
@interface DrawMoneyViewController ()
@property (weak, nonatomic) IBOutlet UIView *addressView;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UIView *numberView;
@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UIView *costView;
@property (weak, nonatomic) IBOutlet UITableView *contentTV;
- (IBAction)explainAction;
- (IBAction)scanAction;
- (IBAction)drawAction;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (weak, nonatomic) IBOutlet UILabel *withdraw_feeLabel;

@property (weak, nonatomic) IBOutlet UILabel *amountLabel;
@property (weak, nonatomic) IBOutlet UILabel *gotoLabel;
@property (weak, nonatomic) IBOutlet UILabel *markLabel;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (nonatomic, strong) NSString *amount;
@property (nonatomic, strong) NSString *forzon_amount;
@property (nonatomic, strong) NSString *max_withdraw_amount;
@property (nonatomic, strong) NSString *min_withdraw_amount;
@property (nonatomic, strong) NSString *amountNum;
@end

@implementation DrawMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressCopyAction:) name:@"addressCopyNoti" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scanDrawMoneyAddressAction:) name:@"scanDrawMoneyAddressNoti" object:nil];
    
    [self.numberTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    [_addressTF setValue:[UIColor colorWithHexString:@"#606378"] forKeyPath:@"_placeholderLabel.textColor"];
    [_numberTF setValue:[UIColor colorWithHexString:@"#606378"] forKeyPath:@"_placeholderLabel.textColor"];
    
    self.addressView.layer.borderColor = RGBColor(69, 72, 90).CGColor;
    self.addressView.layer.borderWidth = 1.0f;
    
    self.numberView.layer.borderColor = RGBColor(69, 72, 90).CGColor;
    self.numberView.layer.borderWidth = 1.0f;
    
    self.costView.layer.borderColor = RGBColor(69, 72, 90).CGColor;
    self.costView.layer.borderWidth = 1.0f;
    
    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];
    
    self.gotoLabel.text=[NSString stringWithFormat:@"0 %@",self.mark];
    self.markLabel.text=[NSString stringWithFormat:@"%@ |",self.mark];
    [self requstData];
    
}
- (void) scanDrawMoneyAddressAction:(NSNotification *)notification{
    self.addressTF.text=notification.object;
    
    
}
- (void) addressCopyAction:(NSNotification *)notification{
    self.addressTF.text=notification.object;
    
    
}
-(void)textFieldDidChange :(UITextField *)textField{
    double textF=[textField.text doubleValue];
    double amount=[self.amountNum doubleValue];
    double withdraw_fee=[self.withdraw_feeLabel.text doubleValue];
    if (textF>amount) {
        self.gotoLabel.text=[NSString stringWithFormat:@"0 %@",self.mark];;
    }else{
        double goNum=textF-withdraw_fee;
        self.gotoLabel.text=[NSString stringWithFormat:@"%f %@",goNum,self.mark];
    }
    
}

- (void)requstData{
    NSDictionary *param= @{@"currency_id" : self.currency_id};
    [LSRequestManager getRequestWithURL:HUOBI(@"api/currency/currencyCoin")
                                  params:param
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         self.amount=[NSString stringWithFormat:@"%@",responseData[@"data"][@"amount"]];
                                         self.forzon_amount=[NSString stringWithFormat:@"%@",responseData[@"data"][@"forzon_amount"]];
                                         double amount1=[self.amount doubleValue];
                                         double forzon_amount1=[self.forzon_amount doubleValue];
                                         double amountContent=amount1-forzon_amount1;
                                         self.amountNum=[NSString stringWithFormat:@"%f",amountContent];
                                         self.amountLabel.text=[NSString stringWithFormat:@"%f %@",amountContent,self.mark];
                                         self.withdraw_feeLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"currency_name"][@"withdraw_fee"]];
                                         self.contentLabel.text=responseData[@"data"][@"currency_name"][@"withdraw_summary"];
                                         self.max_withdraw_amount=[NSString stringWithFormat:@"%@",responseData[@"data"][@"currency_name"][@"max_withdraw_amount"]];
                                         self.min_withdraw_amount=[NSString stringWithFormat:@"%@",responseData[@"data"][@"currency_name"][@"min_withdraw_amount"]];
                                     } else {
                                         self.amountLabel.text=@"";
                                         self.withdraw_feeLabel.text=@"";
                                         self.contentLabel.text=@"";
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     self.amountLabel.text=@"";
                                     self.withdraw_feeLabel.text=@"";
                                     self.contentLabel.text=@"";
                                 }
                                 showHUD:NO];
}

- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)explainAction {
    
    [self performSegueWithIdentifier:@"restrictionSegue" sender:nil];
}

- (IBAction)scanAction {
    
    ScanViewController *vc = [[ScanViewController alloc] init];
    vc.pushType=@"drawMoney";
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
    
    
    
}

- (IBAction)drawAction {
    
//    SafeCertifyView *safeCertifyView = [SafeCertifyView xib];
//    [[UIApplication sharedApplication].keyWindow addSubview:safeCertifyView];
//    __weak typeof(safeCertifyView) weakSafeCertifyView = safeCertifyView;
//    safeCertifyView.phoneTF.text=@"189****1234";
//    safeCertifyView.emailTF.text=@"245****@qq.com";
//    safeCertifyView.sendActionBlock = ^(NSInteger tag) {
//        [weakSafeCertifyView removeFromSuperview];
//
//        if (tag==0) {
//            NSLog(@"发送手机验证码");
//        }else{
//            NSLog(@"发送短信验证码");
//        }
//    };
//    safeCertifyView.submitActionBlock = ^(NSInteger tag) {
//        [weakSafeCertifyView removeFromSuperview];
//
//        if (tag==0) {
//            NSLog(@"确认");
//            
//
//
//
//
//
//
//
//
//
//        }else{
//            NSLog(@"取消");
//        }
//    };
    if (self.addressTF.text.length==0) {
        [MBProgressHUD showError:@"请选择提币地址"];
        return;
    }
    
    double textNum=[self.numberTF.text doubleValue];
    double max_withdraw_amount=[self.max_withdraw_amount doubleValue];
    double min_withdraw_amount=[self.min_withdraw_amount doubleValue];
    double amountNum=[self.amountNum doubleValue];
    NSString*errorContent=[NSString stringWithFormat:@"最小提币数量为%@",self.min_withdraw_amount];
    if (textNum<min_withdraw_amount) {
        [MBProgressHUD showError:errorContent];
        return;
    }
    if (textNum>amountNum) {
        [MBProgressHUD showError:@"超出提币余额"];
        return;
    }
    if (textNum>max_withdraw_amount) {
        [MBProgressHUD showError:@"超出提币上限"];
        return;
    }
    
    
    DrawMoneySureView *drawMoneySureView = [DrawMoneySureView xib];
    [[UIApplication sharedApplication].keyWindow addSubview:drawMoneySureView];
    __weak typeof(drawMoneySureView) weakDrawMoneySureView = drawMoneySureView;
    drawMoneySureView.addressLabel.text=self.addressTF.text;
    drawMoneySureView.monetKindLabel.text=self.mark;
    drawMoneySureView.countLabel.text=self.numberTF.text;
    drawMoneySureView.costLabel.text=[NSString stringWithFormat:@"%@ %@",self.withdraw_feeLabel.text,self.mark];
    drawMoneySureView.possessLabel.text=[NSString stringWithFormat:@"%@ %@",self.gotoLabel.text,self.mark];
    drawMoneySureView.submitActionBlock = ^(NSInteger tag) {
        [weakDrawMoneySureView removeFromSuperview];
        
        if (tag==0) {
            NSLog(@"确认提币");
            
            if ([[UserDefaults objectForKey:K_ISEMAIL]isEqualToString:@"1"]||[[UserDefaults objectForKey:K_ISGOOGLE]isEqualToString:@"1"]){
                UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
                LoginSafeTableViewController *loginSafeCtrl = [sb instantiateViewControllerWithIdentifier:@"LoginSafeTableViewController"];
                loginSafeCtrl.pushType=@"drawMoneyVC";
                loginSafeCtrl.currency_id=self.currency_id;
                loginSafeCtrl.address=self.addressTF.text;
                loginSafeCtrl.amount=self.numberTF.text;
                [self.navigationController pushViewController:loginSafeCtrl animated:YES];
            }else{
                [self submit];
            }
            
            

            
            
            
            
            
        }else{
            NSLog(@"取消提币");
        }
    };
    
    
    
    
    
    
    
}
- (void)submit{
    NSDictionary *param= @{@"currency_id" : self.currency_id,
                           @"amount" : self.numberTF.text,
                           @"address" :self.addressTF.text,
                           
                           };
    [LSRequestManager postRequestWithURL:HUOBI(@"api/withdraw/drawMoney")
                                  params:param
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                         
                                         [self.navigationController popViewControllerAnimated:YES];
                                         
                                         
                                         
                                         
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
}



- (IBAction)closeAction:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}
- (IBAction)pushAddressAction:(UIButton *)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Me" bundle:nil];
    DetailedAddressViewController *detailedAddressCtrl = [sb instantiateViewControllerWithIdentifier:@"DetailedAddressViewController"];
    //    activity.status = 1;
    detailedAddressCtrl.mark=self.mark;
    detailedAddressCtrl.currency_id=self.currency_id;
    detailedAddressCtrl.pushType=@"drawMoney";
    [self.navigationController pushViewController:detailedAddressCtrl animated:YES];
    
    
    
    
    
}
- (IBAction)allAmouneAction {
    self.numberTF.text=self.amountNum;
    
    double textF=[self.numberTF.text doubleValue];
    double amount=[self.amountNum doubleValue];
    double withdraw_fee=[self.withdraw_feeLabel.text doubleValue];
    if (textF>amount) {
        self.gotoLabel.text=[NSString stringWithFormat:@"0 %@",self.mark];;
    }else{
        double goNum=textF-withdraw_fee;
        self.gotoLabel.text=[NSString stringWithFormat:@"%f %@",goNum,self.mark];
    }
    
    
    
    
}
@end
